Skip to content

Conversation

@GTLin08
Copy link
Contributor

@GTLin08 GTLin08 commented Nov 8, 2025

Some SoCs require kernel code to be placed in RAM, which makes link-time optimization (LTO) unsuitable for these files.
Disabling LTO allows the affected code to be linked as separate objects and placed in specific memory regions.
Running kernel code from RAM can improve execution performance, especially for timing-critical routines or context switch paths.

Another commit is to enable KERNEL_NO_LTO when SOC_IT8XXX2_KERNEL_IN_RAM and LTO are enabled on the IT8XXX2 chip.

Comment on lines 25 to 26
# Append the injected code to the end of the file
file(APPEND ${KERNEL_CMAKE} "${INJECT_CODE}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uh, why is this needed? The whole append/remove thing feels a bit hacky to be honest, hope we can find a different solution, you just need the -fno-lto -g in the build command line for the files above right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason is that setting -fno-lto elsewhere (e.g. board-level CMake) didn’t work likely because of build order issues. Kernel sources were still built with LTO, so I had to apply it directly here to make sure those files compile without LTO and can reside in RAM.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok so this is basically touching the source file to somehow convince cmake to build it again with the new flag?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's correct, it temporarily modifies the kernel CMake file to ensure those specific sources are rebuilt with -fno-lto. After the build configuration is complete, the injected changes are automatically cleaned up to restore the original file state.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. I don't really think it's a good idea to try and modify non generated files from the build script, @tejlmand do you have a better approach to suggest?

Would we not be able to get away with adding a Kconfig flag and the options in the kernel CMakeLists.txt file directly? If this SoC has a scenario where having a kernel specific option to selectively not do LTO it may be handy for other devices too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of modifying the kernel's CMake file, what about creating a separate CMake file that defines the no-LTO and other desired properties?

You could have the kernel CMake file do a conditional include based on a CONFIG option being set.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @fabiobaltieri and @keith-zephyr for the helpful suggestions.
I’ve updated the implementation accordingly. It now adds a Kconfig option to control the behavior, and the logic is handled inside kernel/CMakeLists.txt without modifying it from external scripts. Additionally, I’ve added a separate kernel_no_lto.cmake file to define the no-LTO settings.

@GTLin08 GTLin08 force-pushed the it8xxx2_kernel_in_ram_lto branch from d69e872 to e660d0d Compare November 11, 2025 06:46
@GTLin08 GTLin08 changed the title it8xxx2: ensure kernel in ram code section kernel: Add Kconfig option to disable LTO for kernel sources Nov 11, 2025
@GTLin08 GTLin08 force-pushed the it8xxx2_kernel_in_ram_lto branch from e660d0d to 123ab0b Compare November 11, 2025 10:08
fabiobaltieri
fabiobaltieri previously approved these changes Nov 11, 2025
Comment on lines 25 to 27
NOT src MATCHES "errno\\.c$" AND
NOT src MATCHES "fatal\\.c$" AND
NOT src MATCHES "init\\.c$")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these files be added to the LTO_ALLOWLIST as the default settings?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I’ve added them to the default LTO_ALLOWLIST. Thanks!

@GTLin08
Copy link
Contributor Author

GTLin08 commented Nov 13, 2025

@fabiobaltieri Please revisit, thanks!

@GTLin08 GTLin08 force-pushed the it8xxx2_kernel_in_ram_lto branch from d33aafc to 7171255 Compare November 13, 2025 06:40
fabiobaltieri
fabiobaltieri previously approved these changes Nov 13, 2025
Copy link
Member

@fabiobaltieri fabiobaltieri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tejlmand does this look like an acceptable approach?

@npitre
Copy link

npitre commented Nov 13, 2025 via email

kernel/Kconfig Outdated
help
List of kernel source filenames that should retain LTO even when
CONFIG_KERNEL_NO_LTO is enabled.
Example: CONFIG_KERNEL_LTO_ALLOWLIST="init.c errno.c fatal.c"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you want to make these files the default setting? Otherwise you could expand the help text to suggest this list as a good default as these features don't benefit from the performance gain of running in RAM.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we set default "init.c errno.c fatal.c" for CONFIG_KERNEL_LTO_ALLOWLIST, users would still need to override the entire string when adding more files. Kconfig does not support appending to the default value, so it cannot be extended incrementally.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Acknowledged. As a follow up PR, you could enable CONFIG_KERNEL_NO_LTO on the ITE eval board so that there is a reference implementation for users to copy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, once this PR is merged, I’ll create a follow-up PR to enable.

@GTLin08
Copy link
Contributor Author

GTLin08 commented Nov 14, 2025

Just wondering... Can't you do a partial linking with LTO of the kernel and the application separately, then join them with a final link?

@npitre , Thanks for the suggestion. My concern is that even with partial linking, once LTO is applied, the compiler may still inline or merge functions across the participating kernel files. This means I can no longer obtain the original libkernel object files or their sections that must be placed in RAM.

My current approach explicitly applies -fno-lto to the RAM-critical kernel sources, which guarantees their object layout remains intact. This is required so we can reliably place those routines into RAM for performance.

keith-zephyr
keith-zephyr previously approved these changes Nov 14, 2025
@JarmouniA JarmouniA requested a review from nordicjm November 16, 2025 10:10
@GTLin08 GTLin08 dismissed stale reviews from keith-zephyr and fabiobaltieri via ed42e77 November 17, 2025 03:03
@GTLin08 GTLin08 force-pushed the it8xxx2_kernel_in_ram_lto branch from 7171255 to ed42e77 Compare November 17, 2025 03:03
fabiobaltieri
fabiobaltieri previously approved these changes Nov 17, 2025
keith-zephyr
keith-zephyr previously approved these changes Nov 17, 2025
@GTLin08
Copy link
Contributor Author

GTLin08 commented Nov 18, 2025

@tejlmand, Any feedback would be greatly appreciated.

kernel/Kconfig Outdated
for timing-critical routines or context switch paths.

if KERNEL_NO_LTO
config KERNEL_LTO_ALLOWLIST
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be done in CMake using functions not Kconfig

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion. I’ve removed the KERNEL_LTO_ALLOWLIST Kconfig option and switched to using a CMake function as recommended.

Some SoCs require kernel code to be placed in RAM, which makes
link-time optimization (LTO) unsuitable for these files.
Disabling LTO allows the affected code to be linked as separate
objects and placed in specific memory regions.

Running kernel code from RAM can improve execution performance,
especially for timing-critical routines or context switch paths.

Signed-off-by: Tim Lin <tim2.lin@ite.corp-partner.google.com>
Select KERNEL_NO_LTO only when LTO is enabled. This ensures proper
handling when kernel code is placed in RAM.

Signed-off-by: Tim Lin <tim2.lin@ite.corp-partner.google.com>
@GTLin08 GTLin08 dismissed stale reviews from keith-zephyr and fabiobaltieri via 5e9e169 November 21, 2025 11:00
@GTLin08 GTLin08 force-pushed the it8xxx2_kernel_in_ram_lto branch from ed42e77 to 5e9e169 Compare November 21, 2025 11:00
@sonarqubecloud
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.